import { isInArray } from "@/utils"
import { isPWR } from "./constant"

/* 超能powercreep相关任务 */
export default class PowerCreepMisson extends Room {
    /* home类型Pc任务管理器 */
    public HomeRoleMisson():void{
        if (this.controller.level < 8) return
        var storage_ = global.Stru[this.name]['storage'] as StructureStorage
        if (!storage_) return
        var pc = Game.powerCreeps[`${this.name}/home/${Game.shard.name}/1`]
        var pcspawn = global.Stru[this.name]['powerspawn'] as StructurePowerSpawn
        if (!pc)
            return
        else
        {
            /* 看看是否存活，没存活就孵化 */
            if (!pc.ticksToLive && pcspawn)
            {
                pc.spawn(pcspawn)
                return
            }
            this.operate_extension()
            this.enhance_spawn()
            this.enhance_storage()
            this.enhance_tower()
            this.enhance_lab()
        }
        
        
    }


    /* 挂载增强storage的任务 */
    public enhance_storage():void{
        if (Game.time % 7) return
        var storage_ = global.Stru[this.name]['storage'] as StructureStorage
        if (!storage_) return
        let pc = Game.powerCreeps[`${this.name}/home/${Game.shard.name}/1`]
        if (!pc || !pc.powers[PWR_OPERATE_STORAGE]) return
        
        let effectDelay:boolean = false
        if (!storage_.effects) storage_.effects = []
        for (var effect_ of storage_.effects)
        {
            if (effect_.effect == PWR_OPERATE_STORAGE && effect_.ticksRemaining < 50)
            effectDelay = true
        }
        if ((!storage_.effects ||storage_.effects.length <=0 || effectDelay) && this.MissonNum('PowerCreep','仓库扩容') <= 0)
        {
            /* 发布任务 */
            var thisTask:MissionModel = {
                name:"仓库扩容",
                delayTick:40,
                range:'PowerCreep',
            }
            thisTask.CreepBind = {'home':{num:1,bind:[]}}
            this.AddMisson(thisTask)
        }
    }

    /* 挂载增强tower的任务 */
    public enhance_tower():void{
        if (Game.time  % 17) return
        let pc = Game.powerCreeps[`${this.name}/home/${Game.shard.name}/1`]
        if (!pc || !pc.powers[PWR_OPERATE_TOWER]) return
        if (this.memory.roomSTATE == 'war')
        {
            var towers_list = []
            if (this.memory.StructureIdData.NtowerID)
            {
                var ntower = Game.getObjectById(this.memory.StructureIdData.NtowerID) as StructureTower
                if (ntower)
                {
                    if (!isPWR(ntower))
                        towers_list.push(ntower.id)
                }
            } 
            if (this.memory.StructureIdData.OtowerID.length > 0) 
            for (var o of this.memory.StructureIdData.OtowerID){
                var otower = Game.getObjectById(o) as StructureTower
                if (otower)
                {
                    if (!isPWR(otower))
                    {
                        towers_list.push(otower.id)
                    }
                }
            }
            if (towers_list.length <=0 || this.MissonNum('PowerCreep','强化防御塔') > 0) return
            /* 发布任务 */
            var thisTask:MissionModel = {
                name:"强化防御塔",
                delayTick:70,
                range:'PowerCreep',
                data:{
                    tower:towers_list
                }
            }
            thisTask.CreepBind = {'home':{num:1,bind:[]}}
            this.AddMisson(thisTask)
        }
    }

    /* 挂载在增强lab的任务 */
    public enhance_lab():void{
        if (Game.time % 5) return
        let pc = Game.powerCreeps[`${this.name}/home/${Game.shard.name}/1`]
        if (!pc || !pc.powers[PWR_OPERATE_LAB] || pc.powers[PWR_OPERATE_LAB].cooldown) return
        var storage_ = global.Stru[this.name]['storage'] as StructureStorage
        if (!storage_) return
        for (var misson of this.memory.Misson['Room'])
        {
            if (misson.name == '资源合成' && misson.data.comData && misson.data.comData.length > 0)
            {
                if (this.MissonNum('PowerCreep','lab加速') > 0) return
                /* 查找是否有没有强化的lab */
                var list = []
                for (var id of misson.data.comData)
                {
                    var lab_ = Game.getObjectById(id) as StructureLab
                    if (lab_ && !isPWR(lab_))
                        list.push(id)
                }
                if (list.length <= 0) return
                var thisTask:MissionModel = {
                    name:"lab加速",
                    delayTick:50,
                    range:'PowerCreep',
                    data:{
                        lab: list
                    }
                }
                thisTask.CreepBind = {'home':{num:1,bind:[]}}
                this.AddMisson(thisTask)
            }
        }
    }

    /* 挂载op extension的任务 */
    public operate_extension():void{
        if (Game.time % 11) return
        let pc = Game.powerCreeps[`${this.name}/home/${Game.shard.name}/1`]
        if (!pc || !pc.powers[PWR_OPERATE_EXTENSION] || pc.powers[PWR_OPERATE_EXTENSION].cooldown) return
        var storage_ = global.Stru[this.name]['storage'] as StructureStorage
        if (!storage_ || storage_.store.getUsedCapacity('energy') < 10000) return
        if (this.energyAvailable < this.energyCapacityAvailable*0.4 && this.MissonNum('PowerCreep','操作扩展') <= 0)
        {
            var thisTask:MissionModel = {
                name:"操作扩展",
                delayTick:50,
                range:'PowerCreep',
                data:{
             
                }
            }
            thisTask.CreepBind = {'home':{num:1,bind:[]}}
            this.AddMisson(thisTask)
        }
    }
    /* 挂载op spawn的任务 */
    public enhance_spawn():void{
        if (Game.time % 11) return
        let pc = Game.powerCreeps[`${this.name}/home/${Game.shard.name}/1`]
        if (!pc || !pc.powers[PWR_OPERATE_SPAWN] || pc.powers[PWR_OPERATE_SPAWN].cooldown) return
        var storage_ = global.Stru[this.name]['storage'] as StructureStorage
        if (!storage_) return
        /* 判断是否存在重要任务，存在就加速孵化 */
        var bol = false
        for (var i of this.memory.Misson['Creep'])
        {
            if (isInArray(['X-R','双人小队','攻防一体','主动防御',],i.name) && this.MissonNum('PowerCreep','虫卵强化') <= 0)
            {
                var thisTask:MissionModel = {
                    name:"虫卵强化",
                    delayTick:50,
                    range:'PowerCreep',
                    data:{
                 
                    }
                }
                thisTask.CreepBind = {'home':{num:1,bind:[]}}
                this.AddMisson(thisTask)
                return
            }

        }
    }
}
